package com.sharefang.aproject.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabClickListener;
import com.sharefang.aproject.R;
import com.sharefang.aproject.adapter.FragmentAdapter;
import com.sharefang.aproject.fragment.ControlFragment;
import com.sharefang.aproject.fragment.SocietyFragment;
import com.sharefang.aproject.fragment.UserFragment;
import com.sharefang.aproject.utils.UiUtil;
import com.sharefang.aproject.view.MyViewPager;

public class MainActivity extends BaseActivity {
    private BottomBar bottomBar;
    MyViewPager viewPager;

    @Override
    void findView() {
        viewPager = (MyViewPager) findViewById(R.id.viewpager);
    }

    @Override
    void initView() {
        viewPager.setScanScroll(false);
        viewPager.setOffscreenPageLimit(3);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        bottomBar = BottomBar.attach(this, savedInstanceState);
        //设置BottomBar
        bottomBar = BottomBar.attachShy((CoordinatorLayout) findViewById(R.id.myCoordinator),
                findViewById(R.id.viewpager), savedInstanceState);
        bottomBar.setItems(
                new BottomBarTab(R.mipmap.control_icon, "录制"),
                new BottomBarTab(R.mipmap.control_icon, "坊间"),
                new BottomBarTab(R.mipmap.me_icon, "我")
        );
        bottomBar.setActiveTabColor(getResources().getColor(R.color.main_color_green));
        bottomBar.setOnTabClickListener(new OnTabClickListener() {
            @Override
            public void onTabSelected(int position) {
                viewPager.setCurrentItem(position);
                FragmentAdapter adapter = (FragmentAdapter) viewPager.getAdapter();
                if (position == 0) {
                    //录制页面
                    visibleTabLayout(false);
                    visibleRlUserInfo(false);
                    setCenterTitle("录制");
                } else if (position == 1) {
                    visibleTabLayout(true);
                    setTabModeFixed(false);
                    visibleRlUserInfo(false);
                    SocietyFragment societyFragment = (SocietyFragment) adapter.getItem(position);
                    setViewPager(societyFragment.getViewPager());
                    setCenterTitle("自由坊");

                } else if (position == 2) {
                    visibleTabLayout(true);
                    visibleRlUserInfo(true);
                    UserFragment userFragment = (UserFragment) adapter.getItem(position);
                    setViewPager(userFragment.getViewPager());
                    setTabModeFixed(true);

                    setCenterTitle("个人信息");
                }
            }

            @Override
            public void onTabReSelected(int position) {

            }
        });
    }


    @Override
    void setLayout() {
        setContentView(R.layout.activity_main);
    }

    private void setupViewPager(MyViewPager viewPager) {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        fragmentAdapter.addFragment(new ControlFragment(MainActivity.this), "录制");
        fragmentAdapter.addFragment(new SocietyFragment(MainActivity.this), "坊间");
        fragmentAdapter.addFragment(new UserFragment(MainActivity.this), "我");

        viewPager.setAdapter(fragmentAdapter);
    }

    private long mPressedTime = 0;

    /**
     * 双击退出应用程序推荐方案
     */
    @Override
    public void onBackPressed() {
        long mNowTime = System.currentTimeMillis();//获取第一次按键时间
        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差
            UiUtil.showToast(this, "再按一次退出程序");
            mPressedTime = mNowTime;
        } else {//退出程序
            exitApp();
        }
    }

    /**
     * 使用定时器完成双击退出应用程序，有可能出现异常
     */
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            //调用双击退出
//            UiUtil.doubleClickToFinishApp(MainActivity.this);
//        }
//        return false;
//    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Necessary to restore the BottomBar's state, otherwise we would
        // lose the current tab on orientation change.
        bottomBar.onSaveInstanceState(outState);
    }
}
